import { ref, computed, nextTick } from 'vue'
import { defineStore } from 'pinia'
import empData from '../apis/empData';

export const useEmpDataStore = defineStore('empData', () => {
    const tableData = ref([])
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref()
    const citys = ref('')
    // 筛选条件数据
    const ruleForm = ref({
        city: [],
        experience: '',
        salary: '',
        education: '',
        position: '',
        classify: ''
    })
    // 接口参数 搜索条件(含分页信息和筛选信息)
    const pageInfo = ref({
        current: currentPage.value,
        size: pageSize.value,
        experience: ruleForm.value.experience,
        education: ruleForm.value.education,
        salary: ruleForm.value.salary,
        position: ruleForm.value.position,
        city: citys.value,
        classify: ruleForm.value.classify
    })

    const getPageList = async () => {
        pageInfo.value.position = ruleForm.value.position
        pageInfo.value.city = citys.value
        const res = await empData.getEmpDataPageList(pageInfo.value)
        tableData.value = res.data.empDataPage.records
        total.value = res.data.empDataPage.total
    }
    // 重置条件
    const resetForm = async () => {
        ruleForm.value.city = [];
        ruleForm.value.experience = '';
        ruleForm.value.salary = '';
        ruleForm.value.education = '';
        ruleForm.value.classify = '';
        // 重置页码和条数
        currentPage.value = 1
        pageSize.value = 10
        // 手动重新给接口数据赋值
        // TODO（正常应该是会同步赋值的，但这里没有，需要后续研究）
        pageInfo.value.experience = ruleForm.value.experience
        pageInfo.value.education = ruleForm.value.education
        pageInfo.value.salary = '';
        pageInfo.value.classify = '';
        pageInfo.value.current = 1
        pageInfo.value.size = 10
        citys.value = ''

        getPageList()
    }

    return { tableData, getPageList, pageInfo, currentPage, pageSize, total, ruleForm, resetForm, citys }
})
